const {
    sqlog
} = require("./utils/utils");
const $IS_DEBUG = false;
const $IS_NOT_SECURITY = true;
const SqTools = require("./utils/SqTools")
const production_ipAndPort = "https://pltph.suiqiai.com:30318/new/mm_manage"
// const production_ipAndPort = "http://172.16.0.162:18810/mm_manage"
//生产环境 ip和端口
const debug_ipAndPort = "http://172.16.0.162:18810/mm_manage"
//开发环境 ip和端口
const production_staticUrl = "http://hfzbp.com/"
const debug_staticUrl = "http://hfzbp.com/"
//静态文件


let Urls = {
    is_debug: $IS_DEBUG,
    is_security: !$IS_NOT_SECURITY,
    server: (new SqUrlContainer("")).ipAndPort,
    staitcurl: $IS_DEBUG ? debug_staticUrl : production_staticUrl,
    path(path) {
        let ipAndPort = production_ipAndPort;
        if ($IS_DEBUG) {
            ipAndPort = debug_ipAndPort;
        }
        return new SqUrl(ipAndPort + "/" + path);
    },
    user: new SqUrlContainer('user').as({
        wxlogin: "wxlogin",
        updateuserinfo: "updateuserinfo",
        bind: "bind",
        login: "login"
    }),
    wx: new SqUrlContainer('wx').as({
        decode: "decode",
    }),
    material: new SqUrlContainer("material").as({
        getAllByStoreId: "getAllByStoreId",
        getCanCollectList: "getCanCollectList"
    }),
    collectionOrder: new SqUrlContainer("collectionOrder").as({
        createOrder: "createOrder",
        getOrdersByUserId: 'getOrdersByUserId'
    }),
    upload: new SqUrlContainer("file").as({
        upload: "upload"
    }),
    sign: new SqUrlContainer("sign").as({
        healthCode: "healthCode",
        sign: "sign",
        today: "today",
        signList: "signList",
        checkResult: 'checkResult'
    }),
    store: new SqUrlContainer("store").as({
        getStoreByLoginUserId: "getStoreByLoginUserId"
        
    })



}

function SqUrl(path, security) {

    this.security = security;
    this.path = path;
    this.url = path;

    this.add = function (value) {
        if (SqTools.isObject(value)) {
            let paramUrl = '';
            for (let key in value) {
                paramUrl += ((paramUrl != '' ? '&' : '') + (key + '=' + value[key]));
            }
            this.url = this.path + "?" + paramUrl;
        } else if (SqTools.isArray(value)) {
            let paramUrl = '';
            for (let key in value) {
                paramUrl += ((paramUrl != '' ? '/' : '') + value[key]);
            }
            this.url = this.path + paramUrl;
        } else {
            this.url = this.path + '/' + value;
        }
        return this;
    }
}

function SqUrlContainer(tagName, customIpAndPort) {
    if (customIpAndPort && customIpAndPort != '') {
        this.ipAndPort = customIpAndPort;
    } else {
        this.ipAndPort = production_ipAndPort;
        if ($IS_DEBUG) {
            this.ipAndPort = debug_ipAndPort;
        }
    }


    this.tagName = tagName;
    this.asu = function (path, isNotSecurity) {
        let isSecurity = true;
        if (isNotSecurity) {
            isSecurity = false;
        }
        let tpath = this.ipAndPort + "/" + tagName + "/" + path;
        let surl = new SqUrl(tpath, isSecurity)
        return surl;
    }
    this.a = function (path, isNotSecurity) {
        this[path] = this.asu(path, isNotSecurity);
        return this;
    }
    this.as = function (urls) {
        for (let key in urls) {
            let url = urls[key];
            if (SqTools.isObject(url)) {
                this[key] = this.asu(url.path, url.is_security);
            } else {
                this[key] = this.asu(urls[key], false);
            }
        }
        return this;
    }
    this.awa = function (path, alias, isNotSecurity) {
        this[alias] = this.asu(path, isNotSecurity);
        return this;
    }
    this.addUrl = function (path) {
        let urlFun = function () {
            return this.ipAndPort + "/" + tagName + "/" + path;
        };
        return urlFun;
    }
    this.au = this.addUrl;
    this.addUrlWithPathVar = function (path) {
        let urlFun = function (variable) {
            return this.ipAndPort + "/" + tagName + "/" + path + "/" + variable;
        };
        return urlFun;
    }
    this.auv = this.addUrlWithPathVar;
    this.addUrlWithParam = function (path) {

        let urlFun = function (param) {
            let paramUrl = '';
            for (let key in param) {
                paramUrl += ((paramUrl != '' ? '&' : '') + (key + '=' + param[key]));
            }
            return this.ipAndPort + "/" + tagName + "/" + path + "?" + paramUrl;
        };
        return urlFun;
    }
    this.aup = this.addUrlWithParam;
    // Default urls
    this.detail = function (id) {
        return this.ipAndPort + "/" + tagName + "/detail/" + id || '';
    }
    this.all = this.asu("all")
    this.create = this.asu("create")
    this.add = this.asu("add")
    this.update = this.asu("update")

}
module.exports = Urls